import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit";
import { notification } from "antd";
import { Ajax } from "../../Utils/AjaxConfig";
type ShoppingCartState = {
  loading: boolean;
  error: string | null;
  items: Array<any>;
};

const initialState: ShoppingCartState = {
  loading: false,
  error: null,
  items: [],
};

// 获取用户的购物车数据
export const getShoppingCart = createAsyncThunk(
  "shoppingcart/getShoppingCartItems",
  async (jwt: string, thunkAPI) => {
    const { data } = await Ajax.get("/api/shoppingCart", {
      headers: {
        Authorization: `bearer ${jwt}`,
      },
    });
    return data.shoppingCartItems;
  }
);

// 添加购物车
type AddShoppingCartPramasProps = {
  jwt: string;
  touristRouteId: string;
};
export const addShoppingCart = createAsyncThunk(
  "shoppingcart/addShoppingCartitems",
  async (params: AddShoppingCartPramasProps, thunkAPI) => {
    const { data } = await Ajax.post(
      "/api/shoppingCart/items",
      {
        touristRouteId: params.touristRouteId,
      },

      {
        headers: {
          Authorization: `bearer ${params.jwt}`,
        },
      }
    );
    return data.shoppingCartItems;
  }
);

// 清空购物车
type clearShoppingCartPramasProps = {
  jwt: string;
  itemIds: Array<number>;
};
export const clearShoppingCart = createAsyncThunk(
  "shoppingcart/clearShoppingCart",
  async (pramas: clearShoppingCartPramasProps, thunkAPI) => {
    return await Ajax.delete(
      `/api/shoppingCart/items/(${pramas.itemIds.join(",")})`,
      {
        headers: {
          Authorization: `bearer ${pramas.jwt}`,
        },
      }
    );
  }
);

// 生成订单
export const checkout = createAsyncThunk(
  "shoppingcart/checkout",
  async (jwt: string, thunkAPI) => {
    const { data } = await Ajax.post("/api/shoppingCart/checkout", null, {
      headers:{
        Authorization: `bearer ${jwt}`
      }
    });
    return data;
  }
);

export const shoopingCartSlice = createSlice({
  name: "shoopingCart-slice",
  initialState,
  reducers: {},
  extraReducers: {
    [getShoppingCart.pending.type]: (state) => {
      state.loading = true;
    },
    [getShoppingCart.fulfilled.type]: (
      state,
      action: PayloadAction<Array<any>>
    ) => {
      state.loading = false;
      state.error = null;
      state.items = action.payload;
    },
    [getShoppingCart.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      state.loading = false;
      state.error = action.payload;
    },

    [addShoppingCart.pending.type]: (state) => {
      state.loading = true;
    },
    [addShoppingCart.fulfilled.type]: (
      state,
      action: PayloadAction<Array<any>>
    ) => {
      state.loading = false;
      state.error = null;
      state.items = action.payload;
      notification["success"]({
        message: "添加成功",
        description: "该商品已添加至购物车",
        duration: 2,
      });
    },
    [addShoppingCart.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      state.loading = false;
      state.error = action.payload;
    },

    [clearShoppingCart.pending.type]: (state) => {
      state.loading = true;
    },
    [clearShoppingCart.fulfilled.type]: (
      state,
      action: PayloadAction<Array<any>>
    ) => {
      state.items = [];
      state.loading = false;
      state.error = null;
      notification["success"]({
        message: "清空成功",
        description: "购物车已清空",
        duration: 2,
      });
    },
    [clearShoppingCart.rejected.type]: (
      state,
      action: PayloadAction<string | null>
    ) => {
      // state.items = [];
      state.loading = false;
      state.error = action.payload;
    },
    [checkout.pending.type]: (state) => {
      state.loading = true;
    },
    [checkout.fulfilled.type]: (state, action) => {
      state.loading = false;
      state.error = null;
      state.items = [];
    },
    [checkout.rejected.type]: (state, action) => {
      state.loading = false;
      state.error = action.payload;
    },
  },
});
